<template>
  <div>
    <el-card>
      <el-row :gutter="100">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query" clearable @clear="">
            <el-button slot="append" icon="el-icon-search" @click="getUserList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addUser">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 数据列表区域 -->
      <el-table :data="tableData" border style="width: 80%">
        <el-table-column type="index" label="id" width="50">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="250">
        </el-table-column>
        <el-table-column prop="phone" label="电话" width="300">
        </el-table-column>
        <el-table-column label="状态" width="250">
          <template slot-scope="scope">

            <el-switch active-value="1" inactive-value="0" v-model="scope.row.state" @change="userState(scope.raw)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色" width="250">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="editUser(userList.id)">修改</el-button>
            <el-button type="danger" @click="open(userList.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页效果-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form :model="addUserForm" :rules="addUserRules" ref="addUserRef" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserForm.password"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addUserForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      //获取用户列表的参数对象
      return {
        dialogTitle:'',
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        userList: [],
        total: 2,
        tableData: [{
            id: 1,
            username: '111',
            phone: '13211111111',
            role: '超级管理员',
            state: "1"
          },
          {
            id: 2,
            username: '111',
            phone: '13211111111',
            role: '超级管理员',
            state: "0"
          }
        ],
        dialogVisible: false,
        addUserForm:{
          username:'',
          password:'',
          phone:''
        },
        editUserForm:{
          username:'',
          password:'',
          phone:''
        },
        addUserRules:{username:[ { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }],
            password:[{required:true,message:'请输入密码',trigger:'blur'},{min:6,max:15,message:'长度在6到15个字符',trigger:'blur'}],
            phone:[{required:true,message:'请输入手机号',trigger:'blur'},{min:11,max:11,message:'长度为11个字符',trigger:'blur'}]}
      }
    },
    created() {
      this.getUserList()
    },
    methods: {
      async getUserList() {
        const {
          data: res
        } = await this.$http.get('users', {
          params: this.queryInfo
        })
        if (res.code !== 200) {
          return this.$message.error('获取用户列表失败！')
        }
        console.log(res)
        this.userList = res.data.users
        this.total = res.data.total

      },
      handleSizeChange(newSize) {
        this.queryInfo.pagesize = newSize
        this.getUserList()
      },
      handleCurrentChange(newPage) {
        this.queryInfo.pagenum = newPage
        this.getUserList()
      },
      async userState(userInfo) {
        console.log(userInfo)
        const {
          data: res
        } = await this.$http.put(`users/${userInfo.id}/state/${userInfo.state}`)
        if (res.code !== 200) {
          userInfo.state = !userInfo.state
          return this.$message.error('更新用户状态失败！')
        }
        this.$message.success('修改用户状态成功')
      },
      async addUser(){
        this.dialogTitle="添加用户"
        this.dialogVisible=true
       const{data,res}= this.$http.post('addUser',{User:this.addUserForm})
       if(res.code=='200'){
         this.$message.success(res.msg)
         this.dialogVisible=false
         this.getUserList()
       }else{
         this.$message.error(res.msg)
         this.dialogVisible=true
       }
      },
      async editUser(){
        this.dialogTitle="编辑用户"
        this.dialogVisible=true
        const{data,res}=this.$http.put('editUser',{User:this.editUserForm})

      },
      async deleteUser(id){
        
      },
      open() {
            this.$confirm('是否确认删除?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.deleteUser(id)
              this.$message({

                type: 'success',
                message: '删除成功!'
              });
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              });
            });
          }


    }
  }
</script>

<style lang="less" scoped>
</style>
